/**********************************************************************************
// Font Source
//
// Creation:	28 Apr 2011
// Updated:		28 Apr 2011
// Compiler:	Visual Studio 2010
//
// Notes:		The Font class makes use of LPD3DXFONT to create fonts 
//				and draw text on screen.
//
/**********************************************************************************/

#include "Font.h"		// include Font class definition
#include "D3DGame.h"	// acess D3DGame device

/**********************************************************************************/

Font::Font(char * fontname, int fontsize)
{
	font = nullptr;

	D3DXCreateFont(
		D3DGame::GetDevice(),		// direct3d device
		fontsize,					// height of characters
		0,							// width of characters
		0,							// weight (from 0 to 1000)
		1,							// mip levels
		false,						// italic
		DEFAULT_CHARSET,			// charset
		OUT_TT_PRECIS,				// output precision
		CLIP_DEFAULT_PRECIS,		// ouput quality
		DEFAULT_PITCH,				// pitch and family of the font
		(LPCTSTR) fontname,			// font face name
		&font);						// pointer to ID3DXFont interface
}

/**********************************************************************************/

Font::~Font()
{
	if (font)
		font->Release();
}

/**********************************************************************************/

void Font::Draw(int x, int y, char * text, Color color, uint width, uint height, uint alignment, float rotation)
{
	// if the font position is a fraction of a pixel (e.g. X=40.55,Y=100.23) 
	// the font will be antialiased and somewhat blurred

	// create a translate vector
	D3DXVECTOR2 translation((float) x, (float) y);

	// create a scale vector
	D3DXVECTOR2 scale(1.0f, 1.0f);
	
	// PI divided by 180
	const double PIdiv180 = 0.0174532925194444;

	// convert angle to radians 
	float theta = float(rotation * PIdiv180);

	// create tranformation matrix
	D3DXMATRIX mat;
	D3DXMatrixTransformation2D(&mat, NULL, 0.0f, &scale, NULL, theta, &translation);

	// use the transformation matrix for the font
	D3DGame::GetRenderer()->SetTransform(&mat);

	// defines the target boundaries for the text
	RECT rect = {0, 0, width, height};

	// if no sizes are specified calculate the required rect 
	// to display all the text in one line
	if (width == 0 || height == 0)
		font->DrawText(nullptr,			// do not need a pointer
			text,						// string to calculate size
			-1,							// null terminated string
			&rect,						// destination rect
			DT_CALCRECT,				// calculate the rect for us
			color.Direct3D());			// text color

	font->DrawText(
		D3DGame::GetRenderer(),			// pointer to ID3DXSprite object
		text,							// string to draw
		-1,								// null terminated string
		&rect,							// destination rect
		alignment,						// alignment options: DT_CENTER, DT_LEFT, DT_RIGHT
		color.Direct3D());				// text color
}

/**********************************************************************************/